package main

import (
	"database/sql"
	"fmt"
	"log"

	_ "github.com/lib/pq"
)

const (
	host     = "localhost"
	port     = 5432
	user     = "admin"
	password = "admin"
	dbname   = "testdb"
)

type Newtypes struct {
	id       int
	filetype string
	path     string
}

func main() {
	psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
		"password=%s dbname=%s sslmode=disable",
		host, port, user, password, dbname)
	db, err := sql.Open("postgres", psqlInfo)
	if err != nil {
		panic(err)
	}
	defer db.Close()

	err = db.Ping()
	if err != nil {
		panic(err)
	}

	fmt.Println("Successfully connected!")

	sqlStatement := `SELECT id,filetype,path FROM data.newtypes;`

	rows, err := db.Query(sqlStatement)
	defer rows.Close()
	if err != nil {
		log.Fatal(err)
	}
	newtypes := []Newtypes{} //使用Slice切片存储返回数据集
	newtypes1 := Newtypes{}
	for rows.Next() {
		err := rows.Scan(&newtypes1.id, &newtypes1.filetype, &newtypes1.path)
		newtypes = append(newtypes, newtypes1) //最佳到slice中
		if err != nil {
			log.Fatal(err)
		}
	}
	rows.Close()
	fmt.Println(newtypes)

}
